package com.bwei.like.mynewstop.activity;

import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bwei.like.mynewstop.MainActivity;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.app.MyApplication;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.xListView.XListView;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseActivity extends SlidingFragmentActivity {

    private WindowManager windowManager;
    private View view;
    public WindowManager.LayoutParams layoutParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBehindContentView(R.layout.sliding_menu_frame);
        initGrayBackGroud();
        if (MyApplication.isNightMode) {
            switchNight();
          //  setBackGroudColor((ViewGroup) getWindow().getDecorView());
            setTextColor((ViewGroup) getWindow().getDecorView());
        }
    }

    //蒙布
    public void switchNight() {
        //夜间模式
        if (MyApplication.isNightMode) {
            windowManager.addView(view, layoutParams);
        } else {
            //白天
            windowManager.removeViewImmediate(view);
        }
    }

    public void setTextColor(ViewGroup view) {
        for (int i = 0; i < view.getChildCount(); i++) {
            if (view.getChildAt(i) instanceof ViewGroup) {
                setTextColor((ViewGroup) view.getChildAt(i));
            } else if ((view.getChildAt(i) instanceof TextView)) {
                TextView textView = (TextView) view.getChildAt(i);
                if (MyApplication.isNightMode)
                    textView.setTextColor(Color.WHITE);
                else
                    textView.setTextColor(0xFF454545);
            }
        }
    }

    //初始化布
    public void initGrayBackGroud() {
        //获取窗口管理
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSPARENT);

        view = new View(this);

        view.setBackgroundResource(R.color.color_window);
    }

    public void setBackGroudColor(ViewGroup view) {
        for (int i = 0; i < view.getChildCount(); i++) {
            if (view.getChildAt(i) instanceof ViewGroup) {
                setBackGroudColor((ViewGroup) view.getChildAt(i));
                    if (MyApplication.isNightMode) {
                        view.getChildAt(i).setBackgroundColor(0xFF454545);
                    } else {
                        view.getChildAt(i).setBackgroundColor(Color.WHITE);
                    }
            }
        }
    }

}
